//
//  HomeGameView.swift
//  d11
//
//  Created by peale on 2018/11/19.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias GameSelectBlock = (_ dict: [String:Any]?) -> Void
class HomeGameView: UIView ,UITableViewDelegate,UITableViewDataSource{
      let  reuseIdentifierID : String = "HomeGameViewCell"
    var gameSelectBlock: GameSelectBlock?
    func callClickBlock(_ block: @escaping GameSelectBlock) {
        gameSelectBlock = block
    }
     var arrayList : [[String:Any]]? //数据列表
    var gameCount = 2
    lazy var tableView:UITableView = {
        let table = UITableView(frame:CGRect(x: 0, y: 0, width: self.width, height:self.height), style:UITableView.Style.plain)
        table.dataSource = self
        table.delegate = self
        //table.layoutMargins = UIEdgeInsets.zero
        table.separatorStyle = .none
        table.backgroundColor  = UIColor.clear
        table.showsVerticalScrollIndicator = false
        table.tableFooterView = UIView.init()
        table.separatorStyle = .none
        table.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        table.estimatedRowHeight = 0
        table.estimatedSectionHeaderHeight = 0
        table.estimatedSectionFooterHeight = 0
        return table
    }()
    
    convenience init(list:[[String:Any]]?,frame:CGRect) {
        self.init(frame: frame)
        self.backgroundColor = UIColor.clear
        self.arrayList  = list
        if self.arrayList == nil {
            self.arrayList = [[String:Any]]()
        }
        
        self.addSubview(self.tableView)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return  1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  self.arrayList?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       // return 206
        if gameCount == 1{
            return 408
        }else if gameCount == 2 {
            return 206
        }else{
            return 132
        }
        
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let bgView = UIView.init()
        bgView.backgroundColor = HexRGBAlpha(0xf4f7f9, 1)
        return bgView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell :HomeGameViewCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID as String) as? HomeGameViewCell
        if cell == nil {
            cell =  HomeGameViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID as String)
        }
        //icon_home_game_bottom 19 高度
        var isBottom = false
        if indexPath.row == self.arrayList!.count-1 {
            isBottom = true
        }
        let dict = self.arrayList?[indexPath.row]
        let show = dict?["show"] as? Bool ?? false
        if show == true {
            print("mingzi：\(dict?["name"] as? String ?? "")")
        }
         if kIsIpad == true{
             cell?.setData(imgUrl: dict?["imgNameIOS_Ipad"] as? String ?? "",isBottom:isBottom,gameId:dict?["id"] as? String ?? "",gameCount: gameCount,isShow: dict?["show"] as? Bool ?? false)
         }else{
             cell?.setData(imgUrl: dict?["imgNameIOS"] as? String ?? "",isBottom:isBottom,gameId:dict?["id"] as? String ?? "",gameCount: gameCount,isShow:dict?["show"] as? Bool ?? false)
        }
       
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if gameSelectBlock != nil {
            gameSelectBlock!(self.arrayList?[indexPath.row])
        }
    }
    
    func reload(row:Int,gameCount:Int) -> Void {
        self.gameCount = gameCount
        self.arrayList = GlobalInfo.platformInfoData?[row]["game_list"] as? [[String : Any]]
       // self.tableView.reloadSections([0], with: UITableViewRowAnimation.fade)
       // self.tableView.reloadSections([0], with: UITableViewRowAnimation.top)
        self.tableView.reloadData()
    }
    
    func starAnimationWithRow(row:Int,gameCount:Int) -> Void{
        self.gameCount = gameCount
        self.arrayList = GlobalInfo.platformInfoData?[row]["game_list"] as? [[String : Any]]
         self.tableView.reloadData()
         TableViewAnimationKit.show(with: XSTableViewAnimationType.init(rawValue: 6)!, tableView: self.tableView)
    }
}
